﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESOL.Common.Entity;
using ESOL.Business.Pur;
using ESOL.Common;

namespace ESOL.WinUI.Pur
{
    public partial class frmManagePurchaseArrivedOrder : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private PurPurchaseArrivedEntity mSelectedPurchaseArrived;
        #endregion

        #region Constructor
        public frmManagePurchaseArrivedOrder()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtArrivedCode.Text) == false)
            {
                condition += " AND PPA.ARRIVED_CODE LIKE '%" + txtArrivedCode.Text + "%'";
            }
            if (dtpArrivedDate.EditValue!=null)
            {
                condition += " AND PPA.ARRIVED_DATE >= CONVERT(DATETIME,'" + ((DateTime)dtpArrivedDate.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            if (dtpArrivedDateTo.EditValue!=null)
            {
                condition += " AND PPA.ARRIVED_DATE <= CONVERT(DATETIME,'" + ((DateTime)dtpArrivedDateTo.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "PPA.ARRIVED_CODE DESC";
            string supplierId = "";
            if (txtSupplier.Tag != null && txtSupplier.Tag.GetType() == typeof(MstSupplierEntity))
            {
                supplierId = ((MstSupplierEntity)txtSupplier.Tag).SupplierId;
            }
            string purchaseOrderId = "";
            if (txtPurchaseOrder.Tag != null && txtPurchaseOrder.Tag.GetType() == typeof(PurPurchaseOrderEntity))
            {
                purchaseOrderId = ((PurPurchaseOrderEntity)txtPurchaseOrder.Tag).PurchaseOrderId;
            }
            DataTable dt = PurPurchaseArrivedBs.GetByCondition(condition, purchaseOrderId, supplierId, orderBy, rowCount);
            gvMain.DataSource = dt;
        }

        private void BindDetailGrid(string arrivedOrderID)
        {
            if (string.IsNullOrEmpty(arrivedOrderID))
            {
                gvDetail.DataSource = null;
                return;
            }
            DataSet ds = PurPurchaseArrivedItemBs.GetDataSetByPurchaseArrivedID(arrivedOrderID);
            if (ds == null || ds.Tables.Count == 0)
            {
                gvDetail.DataSource = null;
            }
            gvDetail.DataSource = ds.Tables[0];
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colUNIT, "UNIT", true);
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedPurchaseArrived = new PurPurchaseArrivedEntity(dr);
            BindDetailGrid(mSelectedPurchaseArrived.ArrivedId);
        }

        protected override void Click_View()
        {
            if (mSelectedPurchaseArrived == null)
            {
                MessageBox.Show("请选择一个到货单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            List<PurPurchaseOrderEntity> list = PurPurchaseOrderBs.GetPurchaseOrderByArrivedID(mSelectedPurchaseArrived.ArrivedId);
            frmMaintainPurchaseArrivedOrder frm = new frmMaintainPurchaseArrivedOrder(list);
            frm.ThisActionType = ActionType.View;
            frm.SelectedPurchaseArrived = mSelectedPurchaseArrived;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedPurchaseArrived == null)
            {
                MessageBox.Show("请选择一个到货单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中到货单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                bool successful = PurPurchaseArrivedBs.DeletePurPurchaseArrived(mSelectedPurchaseArrived);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedPurchaseArrived == null)
            {
                MessageBox.Show("请选择一个到货单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            List<PurPurchaseOrderEntity> list = PurPurchaseOrderBs.GetPurchaseOrderByArrivedID(mSelectedPurchaseArrived.ArrivedId);
            frmMaintainPurchaseArrivedOrder frm = new frmMaintainPurchaseArrivedOrder(list);
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedPurchaseArrived = mSelectedPurchaseArrived;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintainPurchaseArrivedOrder frm = new frmMaintainPurchaseArrivedOrder();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void txtSupplier_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainSupplierQuery frm = new frmMaintainSupplierQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedSupplier != null)
            {
                txtSupplier.Text = frm.SelectedSupplier.SupplierName;
                txtSupplier.Tag = frm.SelectedSupplier;
            }
            else
            {
                txtSupplier.Text = "";
                txtSupplier.Tag = null;
            }
        }

        private void txtPurchaseOrder_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainPurchaseOrderQuery frm = new frmMaintainPurchaseOrderQuery(false);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPurchaseOrder != null)
            {
                txtPurchaseOrder.Text = frm.SelectedPurchaseOrder.PurchaseOrderCode;
                txtPurchaseOrder.Tag = frm.SelectedPurchaseOrder;
            }
            else
            {
                txtPurchaseOrder.Text = "";
                txtPurchaseOrder.Tag = null;
            }
        }

        #endregion


    }
}
